package com.taiyi.renaissancemall.ui.main.first;

import android.content.Intent;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.EmptyBean;
import com.taiyi.renaissancemall.bean.FindBrandListBean;
import com.taiyi.renaissancemall.bean.FirstBean;
import com.taiyi.renaissancemall.bean.FirstClassSwitchBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class FirstPresent extends BasePresent<FirstView> {
    Map<String, String> map = new HashMap<>();

    public void getData() {
        map.clear();
        HttpUtils.postRequest(BaseUrl.GET_FIRST_DATA, this, map, new JsonCallback<BaseResultBean<FirstBean>>() {
            @Override
            public BaseResultBean<FirstBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FirstBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.onSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.onFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FirstBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getClassSwitch(int labelId) {
//        Map<String, String> map = new HashMap<>();
        map.clear();
        map.put("lableid", String.valueOf(labelId));
        HttpUtils.postRequest(BaseUrl.GET_FIRST_CLASS, this, map, new JsonCallback<BaseResultBean<FirstClassSwitchBean>>() {
            @Override
            public BaseResultBean<FirstClassSwitchBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FirstClassSwitchBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getClassSwitchSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    ToastUtils.showShortToast(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FirstClassSwitchBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getBottomThreeClass() {
//        Map<String, String> map = new HashMap<>();
        map.clear();
        map.put("classificationid1", view.getOneTypeId());
        map.put("classificationid3", view.getThreeTypeId());
        HttpUtils.postRequest(BaseUrl.GET_THREE_CLASS, this, map, new JsonCallback<BaseResultBean<FirstBean>>() {
            @Override
            public BaseResultBean<FirstBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FirstBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getClassSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getClassFailuew(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FirstBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getHotSearchContent() {
//        Map<String, String> map = new HashMap<>();
        map.clear();
        HttpUtils.postRequest(BaseUrl.FIND_BRAND_LIST, this, map, new JsonCallback<BaseResultBean<FindBrandListBean>>() {
            @Override
            public BaseResultBean<FindBrandListBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<FindBrandListBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
//                    ToastUtils.showLongToast(response.body().msg_cn);
                    view.getSearchContentSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getSearchContentFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<FindBrandListBean>> response) {
                super.onError(response);
            }
        });
    }


    public void getTopLabel() {
//        Map<String, String> map = new HashMap<>();
        map.clear();
        map.put("type", "3");
        HttpUtils.postRequest(BaseUrl.GET_FIRST_TOP_LABEL, this, map, new JsonCallback<BaseResultBean<EmptyBean>>() {
            @Override
            public BaseResultBean<EmptyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getTopLabelSuccess(response.body().data);
                } else {
                    view.getTopLabelFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<EmptyBean>> response) {
                super.onError(response);
            }
        });
    }

}
